package com.elinkway.infinitemovies.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;

import com.elinkway.infinitemovies.adapter.LiveFragmentStateAdapter;
import com.elinkway.infinitemovies.async.RequestLiveInfoTask;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.LiveInfos;
import com.elinkway.infinitemovies.view.PagerSlidingTabStrip;
import com.le123.ysdq.R;

/**
 * 直播页面(频道页进入直播页)
 *
 * @author zhangshuo
 */
public class LiveActivity extends BaseLoadingActivity implements RequestResultListener<LiveInfos> {

    private final int PAGE_OFFSET_LIMIT = 1;

    /**
     * 横向的Tab
     */
    private PagerSlidingTabStrip mSildingTabStrip;

    private ViewPager mViewPager;

    private LiveFragmentStateAdapter mLiveStateAdapter;

    private RequestLiveInfoTask mRequestTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLoadingContentView(R.layout.activity_live);
        initView();
        mRequestTask = new RequestLiveInfoTask(this);
        mRequestTask.setmListener(this);
        requestLiveInfo();
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void dealWithUmeng() {

    }

    @Override
    public void dealWithVisitUmeng() {

    }

    /**
     * 请求直播数据信息
     * zhangshuo
     * 2015年1月9日 上午9:17:50
     */
    private void requestLiveInfo() {
        mRequestTask.start();
    }

    private void initView() {
        initViewPager();
    }

    private void initViewPager() {
        mSildingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.live_tabs);
        mViewPager = (ViewPager) findViewById(R.id.live_pager);
        mViewPager.setOffscreenPageLimit(PAGE_OFFSET_LIMIT);

    }

    @Override
    public boolean onRequestFailed() {
        setLoadingFailed();
        return false;
    }

    @Override
    public void onRequestSuccess(int updateId, LiveInfos result) {
        if (null != result && result.getAllInfos().size() > 0) {
            mLiveStateAdapter = new LiveFragmentStateAdapter(getSupportFragmentManager(), this, result);
            mViewPager.setAdapter(mLiveStateAdapter);
            mSildingTabStrip.setViewPager(mViewPager);
            setLoadingComplete();
        } else {
            setLoadingFailed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPreRequest() {

    }

    @Override
    protected void onLoadingRefresh() {
        requestLiveInfo();
    }

    @Override
    protected void onLoading() {
        findViewById(R.id.live_content).setVisibility(View.GONE);

    }

    @Override
    protected void onLoadingFailed() {
        findViewById(R.id.live_content).setVisibility(View.GONE);
    }

    @Override
    protected void onLoadingComplete() {
        findViewById(R.id.live_content).setVisibility(View.VISIBLE);

    }

}
